{extend name="common"}

{block name="content"}
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>修改密码</legend>
</fieldset>

<form class="layui-form" action="" method="">
    <div class="layui-form-item">
        <label class="layui-form-label">原密码</label>
        <div class="layui-input-block">
            <input type="password" name="password" required lay-verify="password" autocomplete="off"
                   placeholder="请输入原密码"
                   class="layui-input" style="width: 80%">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">新密码</label>
        <div class="layui-input-block">
            <input type="password" name="newpassword" required lay-verify="newpassword" autocomplete="off"
                   placeholder="请输入密码" class="layui-input" style="width: 80%">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">重复新密码</label>
        <div class="layui-input-block">
            <input type="password" name="repass" required lay-verify="repass" autocomplete="off" placeholder="请重复密码"
                   class="layui-input" style="width: 80%">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
        <button class="layui-btn" lay-submit="" lay-filter="update">修改</button>
        </div>
    </div>
</form>

{/block}

{block name="script"}
<script>
    layui.use(['form'], function () {
        var form = layui.form;
        var $ = layui.jquery;

        form.verify({
            password: function (value) {
                if (!/^[\S]{6,12}$/.test(value) || value == '') {
                    return "请输入6到12位密码！"
                }
            },
            newpassword: function (value) {
                if (!/^[\S]{6,12}$/.test(value) || value == '') {
                    return "请输入6到12位密码！"
                }
            },
            repass: function (value) {
                if (!/^[\S]{6,12}$/.test(value) || value == '') {
                    return "请输入6到12位密码！"
                }
            }
        });

        form.on('submit(update)', function (data) {
            var load = layer.load();
            $.ajax({
                url: "{:url('User/updatePass')}",
                type: "POST",
                data: data.field,
                dataType: 'json',
                success: function (res) {
                    if (1 == res.code) {
                        layer.msg(res.msg, {icon: 1}, function () {
                            parent.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                    layer.close(load);
                }
            });
            return false;
        });
    });
</script>
{/block}